﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class EmpLeaveList : System.Web.UI.Page
    {
        EmpLeaveBiz empLeaveBiz = new EmpLeaveBiz();
      
        string empCode = "";
        string bTm = "";
        string eTm = "";
        string deptID = "";
        string leType="0";

        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["EmpCode"] != null) empCode = Request.Params["EmpCode"].ToString().Trim();
            if (Request.Params["BTime"] != null) bTm = Request.Params["BTime"].ToString().Trim();
            if (Request.Params["ETime"] != null) eTm = Request.Params["ETime"].ToString().Trim();
            if (Request.QueryString["DeptID"] != null) deptID = Request.QueryString["DeptID"].ToString();
            if (Request.Params["LeaveType"] != null) leType = Request.Params["LeaveType"].ToString().Trim();
            if (!IsPostBack)
            {
                btnAdd.Attributes.Add("onclick", "javascript:ShowLink('EmpLeaveAdd.aspx',800,550);");
                //JSUtility.AddReset(btnClear);
                if (deptID == "")
                    new DepartmentBiz().InitDropDownList(ddlDeptID, "0");//针对一层结构使用
                else
                    new DepartmentBiz().InitDropDownList(ddlDeptID, deptID);//针对一层结构使用
                if (empCode != "")
                {
                    txtSerWord.Text = empCode;
                    if (bTm != "") txtBtime.Text = bTm;
                    if (eTm != "") txtEtime.Text = eTm;
                    BondGvList(empCode, bTm, eTm, deptID,leType,"ALL");
                }
            }
        }
        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Ser))
            {
                JSUtility.Alert(this, "无权限！");
                return;
            }
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string deptID = ddlDeptID.SelectedValue.ToString();
            string leaveType = ddlLeaveType.SelectedValue;
            string leaveState = ddlState.SelectedValue;
          
            if (serWord == "" && bTime == "" && eTime == "" && deptID=="0")
            {
                bTime = System.DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");
                    txtBtime.Text = System.DateTime.Now.AddMonths(-6).ToString("yyyy-MM-dd");

                eTime = System.DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
                    txtEtime.Text = System.DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
            }
            BondGvList(serWord, bTime, eTime, deptID, leaveType, leaveState);

        }

        protected void BondGvList(string serWord, string bTime, string eTime, string deptID, string leaveType, string leaveState)
        {
            DataTable dt = empLeaveBiz.GetLists(serWord, bTime, eTime, deptID, leaveType, leaveState);
            if (dt.Rows.Count > 0)
                lbAmount.Text = "合计:    " + dt.Rows.Count;
            else
                lbAmount.Text = "";
            gvList.DataSource =dt;
            gvList.DataBind();

        }
      
        ////根据gridview打印
        //protected void btnPrint_Click(object sender, EventArgs e)
        //{
        //    ToolUtility.Printer("人员离职表", gvList);
        //}
      
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Out))
            {
                JSUtility.Alert(this, "无权限！");
                return;
            }
            //ExcelUtility.GridViewToExcel(gvList, "EmpLeave.xls");
            string serWord = txtSerWord.Text.Trim();
            string bTime = txtBtime.Text.Trim();
            string eTime = txtEtime.Text.Trim();
            string deptID = ddlDeptID.SelectedValue.ToString();
            string leaveType = ddlLeaveType.SelectedValue;
            string leaveState = ddlState.SelectedValue;
            ExcelUtility.ExportDsToXls(this, empLeaveBiz.GetOutLists(serWord, bTime, eTime, deptID, leaveType, leaveState), "Leave.xls");
        }


        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F3F8F8'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
            }
        }
        /*
         insert into Admin_Emp_Leave (EmpCode,EmpName,DeptName,DutyName,HisStateID,LeaveTime,LeaveType,DayNum,
IsProcedure,Account,InputTime,Statu,Memo,Memo2) 

select EmpCode,EmpName,DeptName,DutyName,HisStateID,LeaveTime,LeaveType,DayNum,
IsProcedure,Account,InputTime,Statu,Memo,Memo2 from Admin_Emp_Leave_Delted where EmpCode='01030'
         * */

    }
}